package com.deyinet.bdcms.dao.impl;


import com.deyinet.bdcms.core.dao.impl.BaseDaoImpl;
import com.deyinet.bdcms.dao.ILogsDao;
import com.deyinet.bdcms.entity.Syslog;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Bacel on 2017/04/16.
 */
@Repository
public class LogsDao  extends BaseDaoImpl<Syslog> implements ILogsDao {

    @Override
    public List<Syslog> findByExampleLike(Syslog example, int pageNo, int pageSize) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        criteria.createAlias("sysuserByUserid","user");
        criteria.add(Restrictions.like("user.name",example.getSysuserByUserid().getName()));
        return createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }

    @Override
    public long countByExampleLike(Syslog example) {
        Criteria criteria = getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        Long o = (Long)criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }

    @Override
    public List<Syslog> findByExampleLike(Syslog example, int pageNo, int pageSize, Timestamp beginTime,Timestamp endTime){
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getSysuserByUserid()!=null){
            criteria.createAlias("sysuserByUserid","user");
            criteria.add(Restrictions.like("user.name",example.getSysuserByUserid().getName()));
        }
        if (beginTime!=null&&!"".equals(beginTime)){
            criteria.add(Restrictions.ge("createdate",beginTime));
        }
        if (endTime!=null&&!"".equals(endTime)){
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime()+86400000)));
        }
        return  createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();                 //执行查询结果

    }
    @Override
    public long countByExampleLike(Syslog example, Timestamp beginTime,Timestamp endTime) {
        Criteria criteria = getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getSysuserByUserid()!=null){
            criteria.createAlias("sysuserByUserid","user");
            criteria.add(Restrictions.like("user.name",example.getSysuserByUserid().getName()));
        }
        if (beginTime!=null&&"".equals(beginTime)){
            criteria.add(Restrictions.ge("createdate",beginTime));
        }
        if (endTime!=null&&"".equals(endTime)){
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime()+86400000)));
        }
        Long o = (Long)criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }

}